package com.avic.rong.search.adapter.homePageAdapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager.LayoutParams;
import com.avic.rong.R;
import com.avic.rong.bean.product.GoodBean;
import com.avic.rong.utils.ConstantsUtils;
import com.avic.rong.utils.StringUtils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Creater: Created by wangyz on 2019/9/18.
 * Email: 936804097@qq.com
 */
public class VStandardAdapter extends DelegateAdapter.Adapter<VStandardAdapter.VStandardViewHolder> {


    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private LayoutParams mLayoutParams;
    private List<GoodBean> beanList = new ArrayList<>();

    public void setBeanList(List<GoodBean> beanList) {
        this.beanList = beanList;
    }

    public VStandardAdapter(Context context, LayoutHelper layoutHelper, List<GoodBean> beanList) {
        this(context, layoutHelper, beanList, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    }

    public VStandardAdapter(Context context, LayoutHelper layoutHelper, List<GoodBean> beanList, @NonNull LayoutParams layoutParams) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.beanList = beanList;
        this.mLayoutParams = layoutParams;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public VStandardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

        return new VStandardViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_view_standard, parent, false));
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    @Override
    public void onBindViewHolder(@NonNull VStandardViewHolder vStandardViewHolder, int positon) {
        GoodBean bean = beanList.get(positon);


        String standardNo = "";
        if (!StringUtils.isEmpty(bean.getNo())) {
            standardNo = bean.getNo();
        }
        vStandardViewHolder.tv_standard_name.setText(bean.getTitle().replaceFirst(Pattern.quote(standardNo), ConstantsUtils.EMPTY_STR).trim());

        DecimalFormat df = new DecimalFormat("###0.00");
        String format = df.format(bean.getPrice());

//            String time = bean.getPostTime();
//            String newTime = time.substring(0, time.indexOf("T"));
        if (StringUtils.isEmpty(bean.getAttrShow())) {
            vStandardViewHolder.tv_standard_status.setVisibility(View.GONE);
        } else {
            vStandardViewHolder.tv_standard_status.setVisibility(View.VISIBLE);
            vStandardViewHolder.tv_standard_status.setText(bean.getAttrShow());
        }

        vStandardViewHolder.tv_standard_no.setText(bean.getNo());

        String publishDate = "";
        String implDate = "";
        List<String> attrsList = bean.getAttrs();
        for (String attr : attrsList) {
            if (!StringUtils.isEmpty(attr)) {
                if (attr.contains("60|") && attr.length() > 3) {
                    publishDate = attr.substring(3);
                } else if (attr.contains("61|") && attr.length() > 3) {
                    implDate = attr.substring(3);
                }
            }
        }
        vStandardViewHolder.tv_standard_publish_time.setText("发布日期:  " + publishDate);
        vStandardViewHolder.tv_standard_impl_time.setText("实施日期:  " + implDate);
        vStandardViewHolder.tv_standard_price.setText("¥" + format);

//        standardItemViewHolder.itemView.setOnClickListener(new MyClickListener(standardItemViewHolder, bean));
        //添加购物车
//        standardItemViewHolder.img_shop_car.setOnClickListener(new MyClickListener(standardItemViewHolder, bean));


    }



    public class VStandardViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.tv_standard_status)
        TextView tv_standard_status;
        @BindView(R.id.tv_standard_price)
        TextView tv_standard_price;
        @BindView(R.id.tv_standard_no)
        TextView tv_standard_no;
        @BindView(R.id.tv_standard_name)
        TextView tv_standard_name;
        @BindView(R.id.img_shop_car)
        public ImageView img_shop_car;
        @BindView(R.id.tv_standard_publish_time)
        TextView tv_standard_publish_time;
        @BindView(R.id.tv_standard_impl_time)
        TextView tv_standard_impl_time;


        public VStandardViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        protected void finalize() throws Throwable {
            super.finalize();
        }
    }


}
